package ia1011.zanzibar.rules;

/**
 * 
 * The class represents an element of a regular expression which is a regular 
 * expression followed by a Kleene operator
 *  
 * @author Alessandro Perrino (creator)
 * @author <br> Bastianelli E. emanuele.bastianelli@gmail.com (modifier)
 * @author <br> Caponi E. emiliano.caponi@gmail.com (modifier)
 * @author <br> Castellucci G. castellucci.giuseppe@gmail.com (modifier)
 * @author <br> Rossi A. berardino.viola@gmail.com (modifier)
 *  
 */
public class KleeneElem extends RegExElem {
	FSRegEx body = new FSRegEx(); // The regular expression followed by the kleene operator
	char kleeneop;	// The kleene operator; it must be *, + or ?
	
	
	/**
	 * 
	 * @return inner regular expression on which is applied Kleene operator 
	 */
	public FSRegEx getBody() {
		return body;
	}
	
	/**
	 * 
	 * @return Kleene operator
	 */
	public char getOp() {
		return kleeneop;
	}
	
	/**
	 * Set the inner regular expressioni
	 * @param fsRegEx 
	 */
	public void setBody(FSRegEx fsRegEx) {
		body = fsRegEx;
	}
	
	
	/**
	 *
	 * Sets the Kleene operator
	 * @param kleenOp a character represents Kleene operator. This character must be * or + or ?
	 * @throws Exception throw exception if the character is not * or + or ? 
	 */
	public void setOp(char kleenOp) throws Exception {
		if (kleenOp!='*' && kleenOp!='+' && kleenOp!='?')
			throw new Exception("Invalid Kleene operator");
		else
			kleeneop = kleenOp;
	}
	
	/**
	 * Prints a simple representation of the element. 
	 */
	public void display() {
		System.out.println("{");
		body.display();
		System.out.println("} "+kleeneop);
	}
}
